<?php
class FileController extends Zend_Controller_Action 
{
	public function indexAction()
	{
		$this->_redirect('/file/list/');
	}

	public function listAction()
	{
		$view = new Zend_View();
		$config = Zend::registry('config');
		$root = $this->_getCurrentRepository();
		$dir = $this->_getDirectory();
		$view = new Zend_View();
		$view->setScriptPath($config->general->viewPath);
		$view->addHelperPath($config->general->helperPath);
		$view->dir = $dir;
		$view->root = $root;
		$view->isRoot = ($root->getFilePath()==$dir->getFilePath());
		echo $view->render('listdir.php');
	}

	public function viewAction()
	{
		$file = $this->_getFile();
		$file->send();
	}

	public function thumbAction()
	{
		$file = $this->_getFile();
		$config = Zend::registry('config');
		$cache = App_Cache::getInstance($config->general->cacheDir);
		$id = md5($file->getFilename());
		if (!($cache->test($id))) {
			ob_start();
			$file->sendThumb();
			$serializedImage = ob_get_contents();
			ob_end_clean();
			$cache->save($serializedImage);
		}
		header("Content-Type: image/jpg");
		$thumb = imagecreatefromstring($cache->get($id));
		imagejpeg($thumb,null,100);
	}

	public function createfolderAction()
	{
		$root = $this->_getCurrentRepository();
		$dir = $this->_getDirectory();
		$input = new Zend_Filter_Input($_POST);
		if ($input->testRegex('dirname','/^[^\/\\\\":*?<>|]+$/')) {
			$dirname = $input->getRaw('dirname');
			$dir->createDir(utf8_decode($dirname));
		}
		$this->_redirect($this->_getActionUrl('list',$dir));
	}

	public function uploadAction()
	{
		$root = $this->_getCurrentRepository();
		$dir = $this->_getDirectory();
		if (key_exists('file',$_FILES)) {
			$fileInput = new Zend_Filter_Input($_FILES['file']);
			if ($fileInput->getInt('size')) {
				$tmpFile = new Data_File($fileInput->getPath('tmp_name'));
				$fileName = $tmpFile->getFilename();
				$tmpFile->moveTo($dir);
				$newFile = $dir->getFile($fileName);
				$newFile->rename(utf8_decode($fileInput->getRaw('name')));
			}
		}
		$this->_redirect($this->_getActionUrl('list',$dir));
	}

	public function noRouteAction()
	{
		$this->_redirect('/file/list/');
	}
	
	/**
	 * retrieve current directory using URL parameters
	 *
	 * @return Data_File
	 */
	protected function _getDirectory()
	{
		$path =  $this->_getCurrentRepository()->getPath();
		foreach ($this->_getAllParams() as $key=>$value) {
			$path.= DIRECTORY_SEPARATOR.urldecode($key);
			$path.= DIRECTORY_SEPARATOR.urldecode($value);
		}
		return new Data_Directory($path);
	}

	/**
	 * retrieve current file using URL parameters
	 *
	 * @return Data_File
	 */
	protected function _getFile()
	{
		$path =  $this->_getCurrentRepository()->getPath();
		foreach ($this->_getAllParams() as $key=>$value) {
			$path.= DIRECTORY_SEPARATOR.urldecode($key);
			if ($value!='') {
				$path.= DIRECTORY_SEPARATOR.urldecode($value);
			}
		}
		return new Data_File($path);
	}
	
	/**
	 * translate File system path into url path
	 *
	 * @param string $path
	 * @return string
	 */
	protected function _urlPath($path)
    {
    	$pathTokens = explode(DIRECTORY_SEPARATOR,$path);
		$pathTokens = array_map('urlencode',$pathTokens);
        return implode('/',$pathTokens);
    }
	
    /**
     * Build action url
     * 
     * @param $action string action name
     * @param $file Data_File main file param for this action
     * @return string
     */
    protected function _getActionUrl($action,Data_File $file)
    {
		$root = $this->_getCurrentRepository();
		$append='';
		if (is_a($file,'Data_Directory')) {
			$append.='/';
    	}
		return "/file/$action".$this->_urlPath($file->getRelativePathFrom($root)).$append;
    }
    
    /**
     * return current repository (if existing)
     *
     * @return Data_Directory
     */
    protected function _getCurrentRepository() {
		$config = Zend::registry('config');
		return new Data_Directory($config->file->fileRepositoryList->fileRepository->path);
    }
	
}

?>